package com.qf.web.controller;

import com.qf.service.UserService;
import com.qf.service.impl.UserServiceImpl;
import com.qf.utils.Base64Utils;
import com.qf.utils.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/activate")
public class ActivateServlet extends HttpServlet {  //就是一个激活的servlet，不用继承baseservlet
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");//自己处理乱码
        //获取前端数据
        String email = request.getParameter("e");
        String code = request.getParameter("c");
        //判断
        if(StringUtils.isEmpty(email)||StringUtils.isEmpty(code)){
            request.setAttribute("msg","激活失败");
            request.getRequestDispatcher("/message.jsp").forward(request,response);
            return;//结束方法
        }
        //解码  加密了因为
        email= Base64Utils.decode(email);
        code= Base64Utils.decode(code);
        //激活
        try {
            UserService userService=new UserServiceImpl();
            userService.active(email,code);
            //转发重定向
            request.setAttribute("msg","激活成功");
            request.getRequestDispatcher("/message.jsp").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
            //转发重定向
            request.setAttribute("msg","激活失败");
            request.getRequestDispatcher("/message.jsp").forward(request,response);
        }

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}


